package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.LikesService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.common.constants.RedisConstants;
import com.heima.common.exception.CustomException;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.common.JdkSerializeUtil;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-11-22 9:28
 */
@Service
@Slf4j
public class LikesServiceImpl implements LikesService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Override
    public void likes(Long articleId, Short operation) {
        //点赞
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            //没登录
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        //1.获取用户id
        Integer userId = user.getId();

        //2.存储到redis
        //String(key value)  List(双向链表 队列 先进先出  栈 先进后出)
        //Set 无序集合，去重 Hash 类似于java hashmap 存放多个key value
        //Zset 有序集合 去重 排序 -> 排行榜
        // set名字 文章id  值 用户id10:19

//        if(operation == 0){
//            //点赞
//            stringRedisTemplate.opsForSet().add(RedisConstants.LIKES_KEY + articleId.toString()
//                    ,userId.toString());
//
//
//        }else if(operation == 1){
//            stringRedisTemplate.opsForSet().remove(RedisConstants.LIKES_KEY + articleId.toString()
//                    ,userId.toString());
//        }

        BoundSetOperations<String, String> bound =
                stringRedisTemplate.boundSetOps(RedisConstants.LIKES_KEY + articleId.toString());

        UpdateArticleMess updateArticleMess = new UpdateArticleMess();
        updateArticleMess.setArticleId(articleId);
        updateArticleMess.setType(UpdateArticleMess.UpdateArticleType.LIKES);

        if (operation == 0) {
            Boolean member = bound.isMember(userId.toString());
            if(member != null && member){
                //如果已经点赞，直接返回 幂等性的校验
                return;
            }
            //点赞
            bound.add(userId.toString());

            updateArticleMess.setAdd(1);

        } else if (operation == 1) {
            Boolean member = bound.isMember(userId.toString());
            if(!(member != null && member)){
                //如果没有点赞，直接返回
                return;
            }
            //取消点赞
            bound.remove(userId.toString());

            updateArticleMess.setAdd(-1);
        }


        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, articleId.toString(),
                JSON.toJSONString(updateArticleMess)
        );

    }
}
